package framework.mapstruct;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 定义一个提供 bean转换功能 的接口
 *
 * @author ME
 * @date 2020/11/3
 */
@Mapper(unmappedSourcePolicy = ReportingPolicy.WARN)
public interface CarCovertBasic {

    CarCovertBasic INSTANCE = Mappers.getMapper(CarCovertBasic.class);

    @Mappings({
            // 属性名不一致:
            @Mapping(source = "carName", target = "name"),
            // 属性类型不一致: Date --> String
            @Mapping(target = "createTime", expression = "java(framework.mapstruct.MapStructTest.dateToStr(source.getCreateTime()))")
            // source 中存在 target 没有的属性
            // @Mapping(target = "origin", ignore = true)
    })
    CarVo toConvertVo(CarPo source);

    /**
     * 集合类型转换
     *
     * @param source
     * @return
     */
    List<CarVo> toConvertVos(List<CarPo> source);

    /**
     * 多个类型整合成一个类型
     *
     * @param source1
     * @param source2
     * @return
     */
    @Mappings({
            @Mapping(source = "source1.carName", target = "name"),
            @Mapping(target = "createTime", expression = "java(framework.mapstruct.MapStructTest.dateToStr(source1.getCreateTime()))")
    })
    CarVo toConvertVo(CarPo source1, AttributePo source2);
}
